/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tiebreak.UI;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import tiebreak.BL.Training;

/**
 *
 * @author henrikhansen
 */
public class AssignCoachToTeam extends JFrame{
    private TieBreakTrainingTeamsAndCoaches form;
    String coachFirstVar;
    String coachLastVar;
    String coachName;
    int coachid;
    public AssignCoachToTeam(String coachFirst, String coachLast, int coachID) throws SQLException
    {
        coachFirstVar = coachFirst;
        coachLastVar = coachLast;
        coachName = coachLastVar + ", " + coachFirstVar;
        coachid = coachID;
        initComponents();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setTitle("Assign coache to team");
        setSize(getPreferredSize());
        setResizable(false);
        setVisible(true);
    }

    private void initComponents() throws SQLException
    {
        form = new TieBreakTrainingTeamsAndCoaches(coachName, coachid);
        JPanel buttonPanel = new JPanel();
        JButton assign = new JButton("Assign");
        JButton cancel = new JButton("Cancel");
        getContentPane().setLayout(new GridBagLayout());
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(assign);
        buttonPanel.add(cancel);
        GridBagConstraints g = new GridBagConstraints();
        g.weightx=0;
        g.weighty=0;
        g.gridx=0;
        g.gridy=GridBagConstraints.RELATIVE;
        g.insets=new Insets(TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE, 0, TieBreakConstraints.MEDIUMSPACE);
        g.fill=GridBagConstraints.BOTH;
        getContentPane().add(form,g);

        g.insets=new Insets(0, TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE);
        getContentPane().add(buttonPanel,g);

        MouseListener cancle = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                dispose();
            }
        };
        cancel.addMouseListener(cancle);

        MouseListener assignml = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                try {
                    Training.assignCoachToTeams(coachid, form.getTeamID(), "Test");
                } catch (SQLException ex) {
                    Logger.getLogger(AssignCoachToTeam.class.getName()).log(Level.SEVERE, null, ex);
                }
                JOptionPane.showMessageDialog(null, "The team "+ form.getSelectedTeam() + " is now assigned to "+ coachFirstVar + " " + coachLastVar);
                dispose();
            }
        };

        assign.addMouseListener(assignml);
    }
}
